package com.hapedu.gegeric.improve;

import java.util.ArrayList;

@SuppressWarnings("all")
public class Generic02 {
    public static void main(String[] args) {
        //new ArrayList<Dog>代表这个集合里面只能存放Dog类型
        //如果编译器发现添加的类型不满足要求时就会报错
        //在遍历的时候，可以直接取出Dog类型而不是 Object
        ArrayList<Dog> arrayList = new ArrayList<Dog>();
        arrayList.add(new Dog("旺财",2));
        arrayList.add(new Dog("大黄",3));
        arrayList.add(new Dog("花花",1));
//        arrayList.add(new Cat("招财猫",8));
        for (Dog dog : arrayList) {
            System.out.println(dog.getAge());
        }
    }
}


class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat{
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}